package com.fuang.stream;

import java.util.Optional;

/**
 * Created by Fuang on 2017-9-23.
 * Optional适合做方法的返回值类型，调用方收到Optional类型的对象好处理判断null等
 */
public class OptionalTest {
    public static void main(String[] args) {
        Optional<String> name = Optional.of("asdf");
        name.ifPresent((value) -> {
            System.out.println("The length of the value is: " + value.length());
        });
        System.out.println(name.isPresent());
        System.out.println(name.get());
        System.out.println(name.equals("asdf"));
        System.out.println(name.orElse("orElse"));
        System.out.println(name.orElseGet(() -> "orElseGet"));
        System.out.println(name.map((value) -> value + "map").get());
        System.out.println(name.filter((value) -> value.equals("asdf1")).isPresent());
        System.out.println(name.flatMap((value) -> Optional.of("asdf1")).get());
    }
}
